home *** CD-ROM | disk | FTP | other *** search
- Path: news1.erols.com!newsmaster@erols.com
- From: "Stephen C. Marney" <scmarney@erols.com>
- Newsgroups: comp.lang.c++
- Subject: Re: what getline returns? Nobody can explain this!!
- Date: Mon, 18 Mar 1996 16:06:49 -0500
- Organization: Techsyn Company
- Message-ID: <314DD069.3D0B@erols.com>
- References: <4ifbk8$6nf@bcarh8ab.bnr.ca>
- NNTP-Posting-Host: as6s48.erols.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=iso-8859-1
- Content-Transfer-Encoding: 8bit
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
-
- getline returns *this but ios (superclass of istream) defines the following overloaded operator:
- operator void* () const;
-
- as follows: (ios.h)
- operator void *() const { if(state&(badbit|failbit) ) return 0; return (void *)this; }
-
- So we are getting an implicit conversion to an int used for the if statement.
-
- MS Visual C++ documentation:
- "An operator that converts a stream to a pointer that can be compared to 0.
- Return Value
- The conversion returns 0 if either failbit or badbit is set in the streamÆs error state.See rdstate for a
- description of the error state masks. A nonzero pointer is not meant to be dereferenced."
-
- whyme wrote:
- >
- > getline member fuction in ifstream is supposed to return the
- > receiving object (that is, ostream&).
- >
- > However, many books contain the code samples like:
- >
- > ifstream in("tmp.txt");
- > while (in.getline(buffer,sizeof(buffer))) { ... }
- >
- > This loop quits only if in.getline returns 0 or NUll;
- > ^^^^^^^
- >
- > but an ostream object is neither 0 nor NULL;
- > the result is an object, not integer or pointer.
- >
- > So how can this loop quit?
- >
- > It works; but no one I know can explain this!!!!
- > Thanks for your help.
-
-